package com.ngc.mall.controller;

import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.BannerDomain;
import com.ngc.mall.param.BannerParam;
import com.ngc.mall.service.IBannerService;
import com.ngc.mall.vo.BannerVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 *
 * @author: chenxiaolin
 * @date 2023/7/12 17:51
**/
@RestController
@RequestMapping("/banner")
public class BannerController {
    @Resource
    private IBannerService iBannerService;


    @PostMapping("")
    @ApiOperation("创建Banner")
    public ResponseResult<Long> createBanner(@RequestBody BannerParam param){
        BannerDomain domain=BannerDomain.builder().name(param.getName()).keyword(param.getKeyword()).note(param.getNote()).isEnabled(param.getIsEnabled()   ).build();
        return ResponseResult.success(iBannerService.createBanner(domain));
    }

    @GetMapping("/list")
    @ApiOperation("获取所有banner")
    public ResponseResult<List<BannerDomain>> selectAllBanner(){
        return ResponseResult.success(iBannerService.selectAllBanner());
    }

    @PutMapping("/{id}")
    @ApiOperation("根据id更新Banner")
    public ResponseResult<Boolean> updateBannerById(@PathVariable Long id,@RequestBody BannerParam param){
        BannerDomain domain=BannerDomain.builder().name(param.getName()).keyword(param.getKeyword()).note(param.getNote()).build();
        return ResponseResult.auto(iBannerService.updateBannerById(id,domain));
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取Banner详情")
    public ResponseResult<BannerVO> getDetailById(@PathVariable Long id){
        return ResponseResult.success(iBannerService.getDetailById(id));
    }

    @PutMapping("/status/{id}")
    @ApiOperation("启用/停用Banner")
    public ResponseResult<Boolean> updateStatusById(@PathVariable Long id, @RequestParam boolean isEnabled){
        return ResponseResult.auto(iBannerService.updateStatusById(id,isEnabled));
    }

    @GetMapping("/banner/by/keyword")
    @ApiOperation("根据关键字查询banner")
    public ResponseResult<List<BannerVO>> getBannerByKeyword(@RequestBody List<String> keywords){
        return ResponseResult.success(iBannerService.getBannerByKeyword(keywords));
    }
}
